﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ContourBuilder.Components;

namespace ITAP.ContourBuilder.Manipulation
{
    public abstract class SerializedCommand : Command
    {
        protected ContourContainer undoContainer;
        protected ContourContainer redoContainer;

        public SerializedCommand(ContourControl control)
            : base(control)
        {
        }

        public override void Execute()
        {
            base.Execute();
//            if(this.control.ContourContainer != null)            
                this.undoContainer = this.control.ContourContainer.Clone() as ContourContainer;
        }

        public override void Undo()
        {
            if (undoContainer != null)
            {
                redoContainer = this.control.ContourContainer.Clone() as ContourContainer;
                this.control.ContourContainer = undoContainer;
            }
        }


        public override void Redo()
        {
            if (redoContainer != null)
            {
                undoContainer = this.control.ContourContainer.Clone() as ContourContainer;
                this.control.ContourContainer = redoContainer;
            }            
        }

        public override void Exit()
        {
            this.redoContainer = this.control.ContourContainer.Clone() as ContourContainer;
            base.Exit();            
        }

        public override bool CanUndo
        {
            get
            {
                return true;
            }
        }

        public override object Clone()
        {
            SerializedCommand clone = (SerializedCommand)Activator.CreateInstance(this.GetType(), new object[] { this.control });
            if (this.undoContainer != null)
                clone.undoContainer = this.undoContainer.Clone() as ContourContainer;
            if (this.redoContainer != null)
                clone.redoContainer = this.redoContainer.Clone() as ContourContainer;
            
            return clone;
        }

        public override void SaveToHistory()
        {
            this.redoContainer = this.control.ContourContainer.Clone() as ContourContainer;
            this.control.Manipulator.AddToHistory(this);
          //  this.undoContainer = this.control.ContourContainer.Clone() as ContourContainer;
        }
    }
}
